package com.lolaage.tracks.content;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

import com.lolaage.tracks.TrackPoint;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-9-7
 * @see : 轨迹的节点表，存储轨迹的经纬度与地理位置信息
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
class TrackPointsDB extends DB
{
    private static final String TAG = "TrackPointsDB";
    public static final String TABLE_NAME = "trackPoints";
    
    public static final String TRACKID = "trackId";
    public static final String LATITUTD = "latitude";
    public static final String LONGITUDE = "longitude";
    public static final String ALTITUDE = "altitude";
    public static final String SPEED = "speed";
    public static final String TIME = "time";
    
    private static int BASE_INDEX = 0;
    private static final int INDEX_ID = BASE_INDEX;
    private static final int INDEX_TRACKID= ++BASE_INDEX;
    private static final int INDEX_LATITUTD = ++BASE_INDEX;
    private static final int INDEX_LONGITUDE = ++BASE_INDEX;
    private static final int INDEX_ALTITUDE = ++BASE_INDEX;
    private static final int INDEX_SPEED = ++BASE_INDEX;
    private static final int INDEX_TIME = ++BASE_INDEX;
    
    public static final String SQL_CREATE = "CREATE TABLE[" + TABLE_NAME + "] ("
        + "[" + _ID + "] INTEGER PRIMARY KEY autoincrement," 
        + "[" + TRACKID+ "] INTEGER NOT NULL, " 
        + "[" + LATITUTD + "] DOUBLE," 
        + "[" + LONGITUDE + "] DOUBLE," 
        + "[" + ALTITUDE + "] DOUBLE," 
        + "[" + SPEED + "] FLOAT ," 
        + "[" + TIME + "] LONG"
        + ");";
    
    /** 获取指定的节点*/
    public TrackPoint getPoint(long pid)
    {
        String section = _ID + "=" + pid;
        SQLiteDatabase db = getDB(false);
        
        Cursor cursor = null;
        try 
        {
            cursor = db.query(TABLE_NAME, null, section, null, null, null, null, null);          
            while(cursor.moveToNext())
            {
                return fill(cursor);
            }
        }catch(SQLiteException e)
        {
            e.printStackTrace();
        }finally 
        {
            if(cursor != null)
            {
                cursor.close();
                cursor = null;
            }
        }
        return null;
    }
    
    /** 添加一个轨迹节点*/
    public long addPoint(TrackPoint point)
    {
        SQLiteDatabase db = getDB(true);
        return db.insert(TABLE_NAME, null, toContentValues(point));
    }
    
    /** 获取轨迹的节点列表*/
    public List<TrackPoint> getTrackPoints(long trackId, int page, int num)
    {
        String section = TRACKID + "=" + trackId;
        String orderBy   = _ID + " asc ";
        String limit   = page > 0 ? page*num + "," + num : null;
        SQLiteDatabase db = getDB(false);
        
        Cursor cursor = null;
        try 
        {
            cursor = db.query(TABLE_NAME, null, section, null, null, null, orderBy, limit);
            List<TrackPoint> points = new ArrayList<TrackPoint>(cursor.getCount());
            while(cursor.moveToNext())
            {
                points.add(fill(cursor));
            }
            return points;
        }catch(SQLiteException e)
        {
            e.printStackTrace();
        }finally 
        {
            if(cursor != null)
            {
                cursor.close();
                cursor = null;
            }
        }
        return null;
    }
    
    /** 删除轨迹的节点列表*/
    public boolean deletePoints(long trackId)
    {
        String section = TRACKID + "=" + trackId;       
        return clear(TABLE_NAME, section);
    }
    
    public static ContentValues toContentValues(TrackPoint point)
    {
        ContentValues cv = new ContentValues();
        
        cv.put(TRACKID, point.trackId);
        cv.put(LATITUTD, point.latitude);
        cv.put(LONGITUDE, point.longitude);
        cv.put(ALTITUDE, point.altitude);
        cv.put(SPEED, point.speed);
        cv.put(TIME, point.time);
        
        return cv;
    }
    
    private static TrackPoint fill(Cursor cursor)
    {       
       TrackPoint track = new TrackPoint();
        
       track._id = cursor.getLong(INDEX_ID);
       track.trackId = cursor.getLong(INDEX_TRACKID);
       track.latitude = cursor.getDouble(INDEX_LATITUTD);
       track.longitude = cursor.getDouble(INDEX_LONGITUDE);
       track.altitude = cursor.getDouble(INDEX_ALTITUDE);
       track.speed = cursor.getFloat(INDEX_SPEED);
       track.time = cursor.getLong(INDEX_TIME);
       
       return track;
    }
}
